﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GenUtil.Dto;
using ServiceLayer.Service;
using GenUtil;

namespace WebApp
{
    public partial class Customer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                LoadGrid();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                CustomerDto customerDto = formToDto();
                ServiceLocator.getCustomerService().InsertCustomerDto(customerDto);
                LoadGrid();
            }

        }

        private CustomerDto formToDto()
        {
            CustomerDto customerDto = new CustomerDto();
            customerDto.FirstName = txtFirstName.Text.Trim();
            customerDto.LastName = txtLastName.Text.Trim();
            customerDto.EmailId = txtEmailId.Text.Trim();
            customerDto.PhoneNo = txtPhoneNo.Text.Trim();
            customerDto.Address1 = txtAdress1.Text.Trim();
            customerDto.Address2 = txtAddres2.Text.Trim();
            customerDto.City = txtCity.Text.Trim();
            customerDto.State = txtState.Text.Trim();
            return customerDto;
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        private void LoadGrid()
        {
            List<CustomerDto> customerDtos = ServiceLocator.getCustomerService().getCustomerDtos();
            gvCustomer.DataSource = customerDtos;
            gvCustomer.DataBind();
        }

        protected void btnQuit_Click(object sender, EventArgs e)
        {
            //Settings.SessionContext.CurrentAction = " ";
            //Response.Redirect("~/Enterprise/Default.aspx");
        }
    }
}